package com.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.dao.uservehicleDao;
import com.dao.userDao;
import com.dao.usersDao;
import com.pojo.User;
import com.pojo.Users;
import com.pojo.Uservehicle;
import com.pojo.vo.vo_ResponseBody;
import com.service.RegService;
import com.utils.JedisClient;
import com.utils.jedis.JedisClientUser;

@Service("regService")
public class RegServiceImpl implements RegService {

	@Autowired
	private userDao userDao;

	@Autowired
	private usersDao usersDao;

	@Autowired
	private uservehicleDao uservehicleDao;

	private JedisClient jedisClient;

	@Override
	public String reg(String phoneImei, String code, User user, String userType_id, Uservehicle uservehicle) {// 注册服务
		jedisClient = new JedisClientUser();
		boolean enable=Integer.parseInt(userType_id)==3?false:true; // 车主设为false，需要认证
		boolean isInsert = false;
		String realCode = jedisClient.get(phoneImei + user.getUser_tel());// 获取已发送的验证码
		if (realCode == null) {// 验证码未空则过期
			return JSONArray.toJSONString(new vo_ResponseBody(0, -1, "请发送验证码"));
		} else if (realCode.equals(code)) {// 验证码验证成功
			jedisClient.del(phoneImei + user.getUser_tel());// 比对成功从redis中删除短信验证码
			if (Integer.parseInt(userType_id) == 1 && !user.getUser_tel().equals("18581903441")) {// 管理员
				return JSONArray.toJSONString(new vo_ResponseBody(0, -1, "您没有权限注册管理员"));
			}
			User hasUser = userDao.getUserByUser_cardId(user.getUser_cardId());
			if (hasUser != null) {// 如果该用户已注册,查询是否已经注册该服务
				Users hasUsers = usersDao.getUsersByUser_idAndType_id(hasUser.getUser_id(),
						Integer.parseInt(userType_id));
				if (hasUsers != null) {// 已注册该服务
					return JSONArray.toJSONString(new vo_ResponseBody(0, -1, "您已注册该服务，请勿重复注册"));
				} else {// 未注册该服务，插入关系表 --->用户服务关系表
					isInsert = usersDao
							.insertOneUsers(new Users(hasUser.getUser_id(), Integer.parseInt(userType_id),enable)) == 1 ? true
									: false;
					hasUsers = usersDao
							.getOneUsersByUsers(new Users(hasUser.getUser_id(), Integer.parseInt(userType_id)));
					checkType_idInsertUservehicle(hasUsers, userType_id, uservehicle);// 插入车辆信息
					return isInsert ? JSONArray.toJSONString(new vo_ResponseBody(0, 0, "注册成功，前往登录"))
							: JSONArray.toJSONString(new vo_ResponseBody(0, -1, "注册失败"));
				}
			} else {// 如果该用户未注册
				user.setUser_age(getAgeInUserCardId(user.getUser_cardId()));// 获得年龄
				user.setUser_sex(getSexInUserCardId(user.getUser_cardId()));// 获得性别
				user.setUser_date(getNowTimeString());
				if (userDao.insertOneUser(user) == 1 ? true : false) {// 插入success
																		// -用户表
																		// ---->插入关系表
																		// --->用户服务关系表
					hasUser = userDao.getUserByUser_cardId(user.getUser_cardId());
					isInsert = usersDao
							.insertOneUsers(new Users(hasUser.getUser_id(), Integer.parseInt(userType_id),enable)) == 1 ? true
									: false;
				}
				Users hasUsers = usersDao
						.getOneUsersByUsers(new Users(hasUser.getUser_id(), Integer.parseInt(userType_id)));
				checkType_idInsertUservehicle(hasUsers, userType_id, uservehicle);// 插入车辆信息
				return isInsert ? JSONArray.toJSONString(new vo_ResponseBody(0, 0, "注册成功，前往登录"))
						: JSONArray.toJSONString(new vo_ResponseBody(0, -1, "注册失败"));
			}
		} else {
			return JSONArray.toJSONString(new vo_ResponseBody(0, -1, "验证码不正确"));
		}
	}

	private String getNowTimeString() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

	}

	public int checkType_idInsertUservehicle(Users users, String userType_id, Uservehicle uservehicle) {
		int inInsert = 0;
		if (Integer.parseInt(userType_id) == 3) {// 代表车主服务
			inInsert = uservehicleDao.insertOneUservehicle(users, uservehicle);
		}
		return inInsert;
	}

	public int getAgeInUserCardId(String user_cardId) {// 获得年龄
		int time = 0;
		try {
			Date ago = new SimpleDateFormat("yyyyMMdd").parse(user_cardId.substring(6, 14));
			time = (int) ((new Date().getTime() - ago.getTime()) / 1000 / 60 / 60 / 24 / 365);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return time;
	}

	public String getSexInUserCardId(String user_cardId) {// 获得性别
		String sex = "男";
		user_cardId.substring(16, 17);
		if (Integer.parseInt(user_cardId.substring(16, 17)) % 2 == 0) {
			sex = "女";
		}
		return sex;

	}

}
